package com.hxd.otc.utils;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import com.hxd.otc.base.BaseApplication;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by lzy on 2018/11/19 0019.
 * description ${刘海屏帮助类}
 */
public class NotchHelper {
    public static boolean isNotch() {
        if (DeviceHelper.isXiaomi()) {
            return isHasMIUINotch();
        } else if (DeviceHelper.isOppo()) {
            return isHasOppoNotch();
        }
        return false;
    }

    public static boolean isHasMIUINotch() {
        return Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1 && getMIUINotchValue("ro.miui.notch") == 1;
    }

    /**
     * 小米刘海屏判断.
     *
     * @return 0 if it is not notch ; return 1 means notch
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static int getMIUINotchValue(String key) {
        int result = 0;
        if (DeviceHelper.isXiaomi()) {
            try {
                ClassLoader classLoader = getContext().getClassLoader();
                @SuppressWarnings("rawtypes")
                Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
                //参数类型
                @SuppressWarnings("rawtypes")
                Class[] paramTypes = new Class[2];
                paramTypes[0] = String.class;
                paramTypes[1] = int.class;
                Method getInt = SystemProperties.getMethod("getInt", paramTypes);
                //参数
                Object[] params = new Object[2];
                params[0] = new String(key);
                params[1] = new Integer(0);
                result = (Integer) getInt.invoke(SystemProperties, params);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static boolean isHasOppoNotch() {
        return getContext().getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    public static int getPopupWindowCorrectHeight() {
        if (!isNotch()) return StatusBarHelper.getStatusbarHeight(getContext());
        if (isHasMIUINotch()) {
            return 2 * getNotchHeightAtXaioMi();
        } else if (isHasOppoNotch()) {
            return StatusBarHelper.getStatusbarHeight(getContext()) + getNotchHeightAtOppo();
        } else {
            return StatusBarHelper.getStatusbarHeight(getContext());
        }
    }

    public static int getNotchHeight() {
        if (!isNotch()) return 0;
        if (isHasMIUINotch()) {
            return getNotchHeightAtXaioMi();
        } else if (isHasOppoNotch()) {
            return getNotchHeightAtOppo();
        } else {
            return 0;
        }
    }

    public static int getNotchHeightAtXaioMi() {
        int resourceId = getContext().getResources().getIdentifier("notch_height", "dimen", "android");
        if (resourceId > 0) {
            return getContext().getResources().getDimensionPixelSize(resourceId);
        } else {
            return 0;
        }
    }

    public static int getNotchHeightAtOppo() {
        if (!isHasOppoNotch()) return 0;
        try {
            String config = SystemProperties.get("ro.oppo.screen.heteromorphism");//444,0:636,76
            if (TextUtils.isEmpty(config)) return 0;
            if (!config.contains(":")) return 0;
            String[] arr = config.split(":");
            int notchTop = Integer.parseInt(arr[0].split(",")[1]);
            int notchBottom = Integer.parseInt(arr[1].split(",")[1]);
            return notchBottom - notchTop;
        } catch (Exception e) {
            return 0;
        }
    }

    public static class SystemProperties {
        public static String get(String key) {
            String value = "";
            Class<?> cls = null;
            try {
                cls = Class.forName("android.os.SystemProperties");
                Method hideMethod = cls.getMethod("get", String.class);
                Object object = cls.newInstance();
                value = (String) hideMethod.invoke(object, key);
            } catch (Exception e) {
                LogUtils.e(e);
            }
            return value;
        }
    }

    public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
    public static final int VIVO_FILLET = 0x00000008;//是否有圆角

    public static boolean hasNotchAtVivo(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class FtFeature = classLoader.loadClass("android.util.FtFeature");
            Method method = FtFeature.getMethod("isFeatureSupport", int.class);
            ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
        } catch (ClassNotFoundException e) {
            LogUtils.e("hasNotchAtVivo ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            LogUtils.e("hasNotchAtVivo NoSuchMethodException");
        } catch (Exception e) {
            LogUtils.e("hasNotchAtVivo Exception");
        } finally {
            return ret;
        }
    }

    private static Context getContext() {
        return BaseApplication.getInstance();
    }
}
